package com.xuecheng.learning.controller;

import com.xuecheng.commons.model.dto.LearningProgressDto;
import com.xuecheng.commons.model.vo.CourseRecordVo;
import com.xuecheng.commons.model.vo.ResourceVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.web.exceptions.BusinessException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.learning.service.CourseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 选课记录前端控制器
 * </p>
 *
 * @author 七安
 */
@Slf4j
@RestController

public class CourseRecordController {

    @Autowired
    private CourseRecordService courseRecordService;

    /**
     * 根据课程发布id查询当前登陆学生的所学课程信息
     *
     * @param coursePubId 课程发布id
     * @return 结果
     */
    @GetMapping("/learnedRecords/myCourseRec/{coursePubId}")
    public ResponseResult<CourseRecordVo> findMyCourseRec(@PathVariable("coursePubId") Long coursePubId) throws BusinessException {
        return courseRecordService.findMyCourseRec(coursePubId);
    }

    /**
     * 保存或更新学生的选课记录
     *
     * @param coursePubId 课程发布id
     * @param userId      用户id
     * @return 结果
     */
    @PostMapping("/courseRecord/saveRecord")
    public ResponseResult saveOrUpdateCourseRec(Long coursePubId, Long userId) throws BusinessException {
        return courseRecordService.saveOrUpdateCourseRec(coursePubId, userId);
    }

    /**
     * 获取播放url
     *
     * @param coursePubId 课程发布id
     * @param mediaId     媒资id
     * @param teachplanId 教学计划id
     * @return url
     */
    @GetMapping("/learnedRecords/resource-url")
    public ResponseResult<ResourceVo> getResourceUrl(Long coursePubId, Long mediaId, Long teachplanId) throws BusinessException {
        return courseRecordService.getResourceUrl(coursePubId, mediaId, teachplanId);
    }

    /**
     * 生成/更新课程发布的学习进度
     * @param learningProgressDto 发布的id，课程大纲id,课程大纲名称
     * @return 结果
     */
    @PostMapping("/learnedRecords/progress")
    public ResponseResult saveProgress(@RequestBody LearningProgressDto learningProgressDto) throws BusinessException {
        return courseRecordService.saveProgress(learningProgressDto);
    }
}
